//
//  ByUserIndexVC.m
//  jinpuyiyuan_ios
//
//  Created by mc on 2017/12/4.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByUserIndexVC.h"

// view
#import "ByViewDetailHeaderCarousel.h"          // 轮播
#import "ByViewIndexAdvert.h"                   // 公告
#import "ByViewIndexSection.h"                  // 组头视图

// cell
#import "ByCellIndexFuction.h"                  // 功能
#import "ByCellIndexHealth.h"                   // 健康资讯
#import "ByCellIndexDoctor.h"                   // 金普名医

// vc
#import "ByUserIndexBranchHospitalVC.h"         // 分院选择
#import "ByIndexHealthDetailVC.h"               // 健康资讯详情
#import "ByUserIndexDoctorDetailVC.h"           // 金普名医详情
#import "ByUserIndexRegistrationVC.h"           // 挂号
#import "ByRegistrationSubjectVC.h"


@interface ByUserIndexVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView      *locationView;        // 定位

@property (nonatomic, strong) ByModelIndexBranch *branch_model; // 用来记录选中的分院model
@end

@implementation ByUserIndexVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"金普医生";
    [self createViews];
    [self createCityLocationView];
    [self addBYItemviews:@[@"测试"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    
}

- (void)rightItemsAction:(NSInteger)rightIndex{
    ByRegistrationSubjectVC *vc = [[ByRegistrationSubjectVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}
#pragma mark - getter
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.backgroundColor = BY_TABLE_COLOR;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            if (@available(iOS 11.0, *)) {
                make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-49);
            }else{
                make.top.equalTo(self.view.mas_top);
                make.bottom.equalTo(self.view.mas_bottom).offset(- 49);
            }
        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
#pragma mark - create views
- (void)createViews{
    CGFloat x = 0,y = 0,width = SCREEN_WIDTH,height = SCREEN_WIDTH / 2 + 30;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
   // 轮播
    ByViewDetailHeaderCarousel *carousel_top = [[ByViewDetailHeaderCarousel alloc] initWithFrame:CGRectMake(x, y, width, height) timerDuration:3];
    [headerView addSubview:carousel_top];
    
    // 公告
    ByViewIndexAdvert *advertView = [[ByViewIndexAdvert alloc] initWithFrame:CGRectMake(x, SCREEN_WIDTH / 2, SCREEN_WIDTH, 30) timerDuration:3];
    [headerView addSubview:advertView];
    self.tableView.tableHeaderView = headerView;
    
}
#pragma mark - create views
// 创建导航栏视图
- (void)createCityLocationView {
    
    self.locationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.3, 30)];
    self.locationView.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationTap:)];
    [self.locationView addGestureRecognizer:tap];
    
    UIImageView *downIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, 20, 20)];
    downIcon.image = [UIImage imageNamed:@"Index_dingwei"];
    [self.locationView addSubview:downIcon];
    downIcon.tag = 1;
    
    UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, self.locationView.width - 20, self.locationView.height)];
    cityLabel.font = FONT(14);
    cityLabel.text = @"选择";
    cityLabel.textColor = [UIColor whiteColor];
    cityLabel.adjustsFontSizeToFitWidth = YES;
    [self.locationView addSubview:cityLabel];
    cityLabel.tag = 2;
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:self.locationView];
    self.navigationItem.leftBarButtonItem = leftItem;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ByCellIndexFuction *cell = [ByCellIndexFuction cellWithTableView:tableView];
        cell.registraionBlock = ^{
            ByUserIndexRegistrationVC *vc = [[ByUserIndexRegistrationVC alloc] init];
            if (self.branch_model) {
                vc.branch_model = self.branch_model;
            }
            [self by_normalPush:vc hasNavi:YES];
        };
        return cell;
    }else if (indexPath.section == 1){
        ByCellIndexHealth *cell = [ByCellIndexHealth cellWithTableView:tableView];
        ByModelIndexHealth *model = [[ByModelIndexHealth alloc] init];
        cell.model = model;
        return cell;
    }else{
        ByCellIndexDoctor *cell = [ByCellIndexDoctor cellWithTableView:tableView];
        ByModelIndexDoctor *model = [[ByModelIndexDoctor alloc] init];
        cell.model = model;
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return SCREEN_WIDTH / 2;
    }
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 5;
    }
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return [UIView new];
    }else{
        NSArray *sectionAry = @[@"健康资讯",@"金普名医"];
        NSArray *imgAry = @[@"Index_jiankang",@"Index_mingyi"];
        ByViewIndexSection *sectionView = [[ByViewIndexSection alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) andTitle:sectionAry[section - 1] andImg:imgAry[section - 1]];
        return sectionView;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 健康资讯
    if (indexPath.section == 1) {
        ByIndexHealthDetailVC *vc = [[ByIndexHealthDetailVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
    //  金普名医
    else if (indexPath.section == 2){
        ByUserIndexDoctorDetailVC *vc = [[ByUserIndexDoctorDetailVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
}
#pragma mark - 分院选择
- (void)locationTap:(UITapGestureRecognizer *)tap {
    ByUserIndexBranchHospitalVC *vc = [[ByUserIndexBranchHospitalVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
    __weak typeof(self) weakSelf = self;
    vc.selectBranchBlcok = ^(ByModelIndexBranch *model) {
        weakSelf.branch_model = model;
        UILabel *cityLabel = [weakSelf.locationView viewWithTag:2];
        cityLabel.text = model.name;
    };
}
@end
